﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class Pages_PO : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetClientDetails();
            BindProducts();
            BindPaymentTerms();
            BindShippingTerms();
            BindShippingType();
            editMode();
          //  bindcurrency();

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        if (ValidateForm() != "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('" + ValidateForm() + "');", true);
            return;
        }

        tbl_purchase_order model = new tbl_purchase_order();
        // model.PO_ID = Convert.ToInt32(txtPO_ID.Text.Trim());
        model.USER_CODE = ddl_User.SelectedValue.ToString();
        model.PURCHASE_ORDER_NO = txtPURCHASE_ORDER_NO.Text.Trim();
        model.IS_SAMPLE = Convert.ToInt32(chkIS_SAMPLE.Checked);
        BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
        if (Session["ProductDetails"] == null)
        {

            Session["ProductDetails"] = bll.GetProducts();
        }
        model.SUB_PRODUCT_ID = Int32.Parse(((DataTable)Session["ProductDetails"]).
            Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString()
            + "' and PURITY_TYPE='" + ddl_Purity.SelectedValue.ToString()
            + "' and PSD_TYPE='" + ddl_Psd.SelectedValue.ToString()
            + "' and SURFACE_AREA_TYPE='" + ddl_Surface.SelectedValue.ToString() + "'"
             )[0]["SUB_PRODUCT_ID"].ToString());

        model.QUANTITY = double.Parse(txtQUANTITY.Text.Trim());
        model.PRICE_PER_KG = double.Parse (txtPRICE_PER_KG.Text.Trim());
        model.PAYMENT_TERM_ID = Convert.ToInt32(ddlPAYMENT_TERM_ID.SelectedValue.Trim());
        model.DELIVERY_DATE = txtDELIVERY_DATE.Text.toDateTimeWithoutError(); //Convert.ToDateTime(txtDELIVERY_DATE.Text.Trim(),).toDateTimeWithoutError();
        model.PARTIAL_PAYMENT = Convert.ToInt32(chkPARTIAL_PAYMENT.Checked);
        model.SHIPPING_ID = Convert.ToInt32(ddlSHIPPING_ID.SelectedValue.Trim());
        model.SHIPPING_TERMS_ID = Convert.ToInt32(ddlSHIPPING_TERMS_ID.SelectedValue.Trim());
        model.PRODUCTION_RELESE_DATE = txtPRODUCTION_RELESE_DATE.Text.toDateTimeWithoutError();    // txtPRODUCTION_RELESE_DATE.Text.Trim().toDateTimeWithoutError();
        model.CREATED_DATE = DateTime.Now;
        model.currencyID = Int32.Parse(ddlCurrency.SelectedValue.ToString());
        if (Request.QueryString["editRecord"] != null)
        {
            model.PO_ID = Int32.Parse(Request.QueryString["editRecord"].ToString());
        }
        model.CREATED_BY = User.Identity.Name;
        //  model.ACCEPTED_BY = txtACCEPTED_BY.Text.Trim();
        //  model.ACCEPTED_DATE = txtACCEPTED_DATE.Text.Trim().toDateTimeWithoutError();


        try
        {
            if (Request.QueryString["editRecord"] != null)
            {
                bll.update(model);
                model.UPDATED_BY = User.Identity.Name;
                model.UPDATED_DATE = DateTime.Now;
            }
            else
            {
                // BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
                bll.insert(model);
            }
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Order Entered Succesfully');", true);
            return;
        }
        catch (Exception)
        {

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
            return;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        // txtPO_ID.Text = "";
        //  txtUSER_CODE.Text = "";
        txtPURCHASE_ORDER_NO.Text = "";
        chkIS_SAMPLE.Checked = false;
        //  ddlSUB_PRODUCT_ID.SelectedIndex = -1;
        txtQUANTITY.Text = "";
        txtPRICE_PER_KG.Text = "";
        ddlPAYMENT_TERM_ID.SelectedIndex = 0;
        txtDELIVERY_DATE.Text = "";
        chkPARTIAL_PAYMENT.Checked = false;
        ddlSHIPPING_ID.SelectedIndex = 0;
        ddlSHIPPING_TERMS_ID.SelectedIndex = 0;
        txtPRODUCTION_RELESE_DATE.Text = "";
        //  txtCREATED_DATE.Text = "";
        //  txtCREATED_BY.Text = "";
        // txtACCEPTED_BY.Text = "";
        // txtACCEPTED_DATE.Text = "";

    }

    //#region BindCurrency
    //private void bindcurrency()
    //{
    //    BLL_tbl_currency_master bll = new BLL_tbl_currency_master();



    //    ddlCurrency.DataSource = bll.selectAll(); ;
    //    ddlCurrency.DataTextField =  bll.selectAll()[0].ToString();
    //    ddlCurrency.DataValueField = bll.selectAll()[1].ToString();
    //    ddlCurrency.DataBind();
    
    //}


    //#endregion

    protected void ddlcurrency_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlCurrency.Items.Insert(0, li);
    }
    

    #region Load Products Details

    private void BindProducts()
    {

        BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
        try
        {
            DataTable dt = new DataTable();
            if (Session["ProductDetails"] == null)
            {

                Session["ProductDetails"] = bll.GetProducts();
            }

            dt = (DataTable)Session["ProductDetails"];
            ddl_ProductName.DataSource = dt.DefaultView.ToTable(true, "PRODUCT_NAME");
            ddl_ProductName.DataTextField = dt.Columns["PRODUCT_NAME"].ToString();
            ddl_ProductName.DataValueField = dt.Columns["PRODUCT_NAME"].ToString();
            ddl_ProductName.DataBind();
            ddl_ProductName.Items.Insert(0, "---Select Product---");
            return;
        }
        catch (Exception)
        {

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Unable to Connect Database');", true);
            return;
        }
        finally
        {
            bll = null;
        }
    }

    #endregion

    #region Product Name Selection Changed
    protected void ddl_ProductName_SelectedIndexChanged(object sender, EventArgs e)
    {

        ddl_Purity.Items.Clear();
        ddl_Surface.Items.Clear();
        ddl_Psd.Items.Clear();
        //ddl_MillType.Items.Clear();

        if (ddl_ProductName.SelectedIndex > 0)
        {
            if (Session["ProductDetails"] == null)
            {
                BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
                Session["ProductDetails"] = bll.GetProducts();
            }

            DataTable dtPurity = ((DataTable)Session["ProductDetails"]).Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString() + "'").CopyToDataTable().DefaultView.ToTable(true, "PURITY_TYPE");
            ddl_Purity.DataSource = dtPurity;
            ddl_Purity.DataTextField = dtPurity.Columns["PURITY_TYPE"].ToString();
            ddl_Purity.DataValueField = dtPurity.Columns["PURITY_TYPE"].ToString();
            ddl_Purity.DataBind();
            ddl_Purity.Items.Insert(0, "-- Select Purity-- ");
            return;
        }
        return;
    }
    #endregion

    #region Purity Selection Changed
    protected void ddl_Purity_SelectedIndexChanged(object sender, EventArgs e)
    {

        ddl_Surface.Items.Clear();
        ddl_Psd.Items.Clear();
     //   ddl_MillType.Items.Clear();
        if (ddl_Purity.SelectedIndex > 0)
        {
            if (Session["ProductDetails"] == null)
            {
                BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
                Session["ProductDetails"] = bll.GetProducts();
            }
            DataTable dtPsd = ((DataTable)Session["ProductDetails"]).Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString() + "' and PURITY_TYPE='" + ddl_Purity.SelectedValue.ToString() + "'").CopyToDataTable().DefaultView.ToTable(true, "PSD_TYPE");
            ddl_Psd.DataSource = dtPsd;
            ddl_Psd.DataTextField = dtPsd.Columns["PSD_TYPE"].ToString();
            ddl_Psd.DataValueField = dtPsd.Columns["PSD_TYPE"].ToString();
            ddl_Psd.DataBind();
            ddl_Psd.Items.Insert(0, "-- Select PSD TYPE-- ");
            return;
        }
        return;
    }
    #endregion

    #region Psd Type Selection Changed
    protected void ddl_Psd_SelectedIndexChanged(object sender, EventArgs e)
    {

        ddl_Surface.Items.Clear();
    //    ddl_MillType.Items.Clear();
        if (ddl_Psd.SelectedIndex > 0)
        {
            if (Session["ProductDetails"] == null)
            {
                BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
                Session["ProductDetails"] = bll.GetProducts();
            }
            DataTable dtSurface = ((DataTable)Session["ProductDetails"]).Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString() + "' and PURITY_TYPE='" + ddl_Purity.SelectedValue.ToString() + "' and PSD_TYPE='" + ddl_Psd.SelectedValue.ToString() + "'").CopyToDataTable().DefaultView.ToTable(true, "SURFACE_AREA_TYPE");
            ddl_Surface.DataSource = dtSurface;
            ddl_Surface.DataTextField = dtSurface.Columns["SURFACE_AREA_TYPE"].ToString();
            ddl_Surface.DataValueField = dtSurface.Columns["SURFACE_AREA_TYPE"].ToString();
            ddl_Surface.DataBind();
            ddl_Surface.Items.Insert(0, "-- Select SURFACE AREA-- ");
            return;
        }
        return;
    }
    #endregion

    #region Surface Area Dropdown Selection Changes
    protected void ddl_Surface_SelectedIndexChanged(object sender, EventArgs e)
    {

     //   ddl_MillType.Items.Clear();

        if (ddl_Surface.SelectedIndex > 0)
        {
            if (Session["ProductDetails"] == null)
            {
                BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
                Session["ProductDetails"] = bll.GetProducts();
            }
            string strgrade = "";
            if (ddl_Purity.SelectedItem.ToString() != "N/A")
            {
                strgrade += ddl_Purity.SelectedItem.ToString();
            }

            if (ddl_Psd.SelectedItem.ToString() != "N/A")
            {
                strgrade += ddl_Psd.SelectedItem.ToString();
            }

            if (ddl_Surface.SelectedItem.ToString() != "N/A")
            {
                strgrade += ddl_Surface.SelectedItem.ToString();
            }

            txtGrade.Text = strgrade;
        //    DataTable dtMill = ((DataTable)Session["ProductDetails"]).Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString() + "' and PURITY_TYPE='" + ddl_Purity.SelectedValue.ToString() + "' and PSD_TYPE='" + ddl_Psd.SelectedValue.ToString() + "' and SURFACE_AREA_TYPE='" + ddl_Surface.SelectedValue.ToString() + "'").CopyToDataTable().DefaultView.ToTable(true, "MILL_TYPE");
        //    ddl_MillType.DataSource = dtMill;
        //    ddl_MillType.DataTextField = dtMill.Columns["MILL_TYPE"].ToString();
        //    ddl_MillType.DataValueField = dtMill.Columns["MILL_TYPE"].ToString();
        //    ddl_MillType.DataBind();
        //    ddl_MillType.Items.Insert(0, "-- Select MILL TYPE-- ");
        //    return;
        }
        return;
    }
    #endregion

    #region Mill Type Selected Index Changed
    //protected void ddl_MillType_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (ddl_MillType.SelectedIndex > 0)
    //    {
    //        if (Session["ProductDetails"] == null)
    //        {
    //            BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
    //            Session["ProductDetails"] = bll.GetProducts();
    //        }
    //        lblStock.Text = ((DataTable)Session["ProductDetails"]).
    //            Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString()
    //            + "' and PURITY_TYPE='" + ddl_Purity.SelectedValue.ToString()
    //            + "' and PSD_TYPE='" + ddl_Psd.SelectedValue.ToString()
    //            + "' and SURFACE_AREA_TYPE='" + ddl_Surface.SelectedValue.ToString()
    //             + "' and MILL_TYPE='" + ddl_MillType.SelectedValue.ToString()
    //            + "'")[0]["CURRENT_STOCK"].ToString() + " K.G.";
    //        return;
    //    }
    //    return;
    //}
    #endregion

    #region Get Client Details

    private void GetClientDetails()
    {
        BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
        try
        {
            DataTable dt = bll.GetClients();
            ddl_User.DataSource = dt;
            ddl_User.DataTextField = dt.Columns["COMPANY_NAME"].ToString();
            ddl_User.DataValueField = dt.Columns["USER_CODE"].ToString();
            ddl_User.DataBind();
            ddl_User.Items.Insert(0, "--Select Customer--");
            return;
        }
        catch (Exception)
        {

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured while fetching Client Details');", true);
            return;
        }
        finally
        {
            bll = null;

        }
    }

    #endregion

    #region Customer Selection Changed
    protected void ddl_User_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ddl_User.SelectedIndex > 0)
        {
            BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
            DataTable dt = bll.GetClients(ddl_User.SelectedValue.ToString());
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    litClientDetails.Text = dt.Rows[0]["ADDRESS"].ToString();
                    return;
                }
            }


        }
        else return;
    }
    #endregion

    #region Bind Payment Terms
    private void BindPaymentTerms()
    {
        BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
        try
        {
            DataTable dt = bll.GetPaymentTerms();
            ddlPAYMENT_TERM_ID.DataSource = dt;
            ddlPAYMENT_TERM_ID.DataTextField = dt.Columns["PAYMENT_TERM"].ToString();
            ddlPAYMENT_TERM_ID.DataValueField = dt.Columns["PAYMENT_TERM_ID"].ToString();
            ddlPAYMENT_TERM_ID.DataBind();
            ddlPAYMENT_TERM_ID.Items.Insert(0, "--Select Payment Term--");
            return;
        }
        catch (Exception)
        {

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured while fetching Client Details');", true);
            return;
        }
        finally
        {
            bll = null;

        }
    }

    #endregion

    #region Shipping Type
    private void BindShippingType()
    {
        BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
        try
        {
            DataTable dt = bll.GetShippingType();
            ddlSHIPPING_ID.DataSource = dt;
            ddlSHIPPING_ID.DataTextField = dt.Columns["SHIPPING_TYPE"].ToString();
            ddlSHIPPING_ID.DataValueField = dt.Columns["SHIPPING_ID"].ToString();
            ddlSHIPPING_ID.DataBind();
            ddlSHIPPING_ID.Items.Insert(0, "--Select Shipping Type--");
            return;
        }
        catch (Exception)
        {

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured while fetching Client Details');", true);
            return;
        }
        finally
        {
            bll = null;

        }
    }

    #endregion

    #region Shipping Terms
    private void BindShippingTerms()
    {
        BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
        try
        {
            DataTable dt = bll.GetShippingTerms();
            ddlSHIPPING_TERMS_ID.DataSource = dt;
            ddlSHIPPING_TERMS_ID.DataTextField = dt.Columns["SHIPPING_TERMS"].ToString();
            ddlSHIPPING_TERMS_ID.DataValueField = dt.Columns["SHIPPING_TERMS_ID"].ToString();
            ddlSHIPPING_TERMS_ID.DataBind();
            ddlSHIPPING_TERMS_ID.Items.Insert(0, "--Select Shipping Term--");
            return;
        }
        catch (Exception)
        {

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured while fetching Client Details');", true);
            return;
        }
        finally
        {
            bll = null;

        }
    }

    #endregion

    #region Validate Form

    private string ValidateForm()
    {
        string str = "";
        try
        {
            // Check if Client is Selected 
            if (ddl_User.SelectedIndex == 0)
            {
                return "Please Select Customer";
            }

            // Check if Product is Selected 
            if (ddl_ProductName.SelectedIndex == 0) return "Please Select Product Name";

            // Check if Purity Type is selected 

            if (ddl_Purity.SelectedIndex == 0) return "Please Select Purity ";

            // Check if Psd Type is selected 

            if (ddl_Psd.SelectedIndex == 0) return "Please select PSD ";

            // Check if Surface Area is Selected 
            if (ddl_Surface.SelectedIndex == 0) return "Please Select Surface Area ";

            // Check if Mill Type is Selected 
            if (ddlCurrency.SelectedIndex == 0) return "Please select currency ";

            // Check if Quantity is valid  (should be greater than 0)
            if (txtQUANTITY.Text == "") return "Please enter Valid Quantity";
            else if (txtQUANTITY.Text != "")
            {
                try
                {
                    double.Parse(txtQUANTITY.Text);
                }
                catch (Exception)
                {
                    return "Please enter Valid Quantity";

                }
            }

            // Check if Price  is valid  (should be greater than 0)
            if (txtPRICE_PER_KG.Text == "") return "Please enter Valid Price (Per KG)";
            else if (txtPRICE_PER_KG.Text != "")
            {
                try
                {
                    double.Parse(txtPRICE_PER_KG.Text);
                }
                catch (Exception)
                {
                    return "Please enter Valid Price ";

                }
            }


            // Check if Payment Term is Selected 
            if (ddlPAYMENT_TERM_ID.SelectedIndex == 0) return "Please Select Payment terms ";

            // Check if Delivery Date is Valid 

            if (txtDELIVERY_DATE.Text == "") return "Please enter Delivery Date ";

           

            // Check if Shipping Type is Selected 
            if (ddlSHIPPING_ID.SelectedIndex == 0) return "Please Select Shiping Type ";

            // Check if Shipping Term is Selected 
            if (ddlSHIPPING_TERMS_ID.SelectedIndex == 0) return "Please select Shipping Term ";

            // Check Production Release date is Valid 

            if (txtPRODUCTION_RELESE_DATE.Text == "") return "Please enter Release Date ";

           
            // Check if Delivery Date is Less than Product Release Data

            try
            {
                DateTime dtProdRelease = txtPRODUCTION_RELESE_DATE.toDateTimeWithoutError();
                DateTime dtDeliverry = txtDELIVERY_DATE.toDateTimeWithoutError();

                if (dtDeliverry < dtProdRelease)
                {
                    return " Production release date should be less than Delivery date ";
                }

            }
            catch (Exception)
            {

                return " Production release date should be less than Delivery date ";
            }

            // Check if Purchase Order Already exists 
            if (Request.QueryString["editRecord"] == null)
            {
                BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
                if (bll.CheckPurchaseOrder(txtPURCHASE_ORDER_NO.Text.Trim()))
                {
                    return "Purchase Order - " + txtPURCHASE_ORDER_NO.Text + " already exists";
                }
            }
           

            return str;
        }
        catch (Exception)
        {

            return "Error in Validation";
        }
        finally
        {
            str = null;
        }
    }

    #endregion


    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_purchase_order model = new tbl_purchase_order();
        model.PO_ID = Convert.ToInt32(id); ;

        BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }

    private void editMode()
    {
        if (Request.QueryString["editRecord"] != null)
        {

            string id = Request.QueryString["editRecord"];
            BLL_tbl_purchase_order bll = new BLL_tbl_purchase_order();
            List<tbl_purchase_order> list = bll.select(Int32.Parse(id));
            foreach (tbl_purchase_order tbl in list)
            {
                ddl_ProductName.SelectedValue = ((DataTable)Session["ProductDetails"]).Select("SUB_PRODUCT_ID= " + tbl.SUB_PRODUCT_ID)[0]["PRODUCT_NAME"].ToString();
                DataTable dtPurity = ((DataTable)Session["ProductDetails"]).Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString() + "'").CopyToDataTable().DefaultView.ToTable(true, "PURITY_TYPE");
                ddl_Purity.DataSource = dtPurity;
                ddl_Purity.DataTextField = dtPurity.Columns["PURITY_TYPE"].ToString();
                ddl_Purity.DataValueField = dtPurity.Columns["PURITY_TYPE"].ToString();
                ddl_Purity.DataBind();
                ddl_Purity.Items.Insert(0, "-- Select Purity-- ");
               ddl_Purity.SelectedValue = ((DataTable)Session["ProductDetails"]).Select("SUB_PRODUCT_ID= " + tbl.SUB_PRODUCT_ID)[0]["PURITY_TYPE"].ToString();

               DataTable dtPsd = ((DataTable)Session["ProductDetails"]).Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString() + "' and PURITY_TYPE='" + ddl_Purity.SelectedValue.ToString() + "'").CopyToDataTable().DefaultView.ToTable(true, "PSD_TYPE");
               ddl_Psd.DataSource = dtPsd;
               ddl_Psd.DataTextField = dtPsd.Columns["PSD_TYPE"].ToString();
               ddl_Psd.DataValueField = dtPsd.Columns["PSD_TYPE"].ToString();
               ddl_Psd.DataBind();
               ddl_Psd.Items.Insert(0, "-- Select PSD TYPE-- ");
               ddl_Psd.SelectedValue = ((DataTable)Session["ProductDetails"]).Select("SUB_PRODUCT_ID= " + tbl.SUB_PRODUCT_ID)[0]["PSD_TYPE"].ToString();

               DataTable dtSurface = ((DataTable)Session["ProductDetails"]).Select("PRODUCT_NAME='" + ddl_ProductName.SelectedValue.ToString() + "' and PURITY_TYPE='" + ddl_Purity.SelectedValue.ToString() + "' and PSD_TYPE='" + ddl_Psd.SelectedValue.ToString() + "'").CopyToDataTable().DefaultView.ToTable(true, "SURFACE_AREA_TYPE");
               ddl_Surface.DataSource = dtSurface;
               ddl_Surface.DataTextField = dtSurface.Columns["SURFACE_AREA_TYPE"].ToString();
               ddl_Surface.DataValueField = dtSurface.Columns["SURFACE_AREA_TYPE"].ToString();
               ddl_Surface.DataBind();
               ddl_Surface.Items.Insert(0, "-- Select SURFACE AREA-- ");
               ddl_Surface.SelectedValue = ((DataTable)Session["ProductDetails"]).Select("SUB_PRODUCT_ID= " + tbl.SUB_PRODUCT_ID)[0]["SURFACE_AREA_TYPE"].ToString();

               string strgrade = "";
               if (ddl_Purity.SelectedItem.ToString() != "N/A")
               {
                   strgrade += ddl_Purity.SelectedItem.ToString();
               }

               if (ddl_Psd.SelectedItem.ToString() != "N/A")
               {
                   strgrade += ddl_Psd.SelectedItem.ToString();
               }

               if (ddl_Surface.SelectedItem.ToString() != "N/A")
               {
                   strgrade += ddl_Surface.SelectedItem.ToString();
               }

               txtGrade.Text = strgrade;

               ddl_User.SelectedValue = tbl.USER_CODE;
               txtPURCHASE_ORDER_NO.Text = tbl.PURCHASE_ORDER_NO;
               txtPURCHASE_ORDER_NO.ReadOnly = true;
               txtQUANTITY.Text = tbl.QUANTITY.ToString();
               txtPRICE_PER_KG.Text = tbl.PRICE_PER_KG.ToString();

               ddlCurrency.DataBind();
               if (tbl.currencyID != null)
                   ddlCurrency.SelectedValue = tbl.currencyID.ToString();

               ddlPAYMENT_TERM_ID.SelectedValue = tbl.PAYMENT_TERM_ID.ToString();
               ddlSHIPPING_ID.SelectedValue = tbl.SHIPPING_ID.ToString();
               ddlSHIPPING_TERMS_ID.SelectedValue = tbl.SHIPPING_TERMS_ID.ToString();
               txtPRODUCTION_RELESE_DATE.Text = tbl.PRODUCTION_RELESE_DATE.ToString("MM/dd/yyyy");
               txtDELIVERY_DATE.Text = tbl.DELIVERY_DATE.ToString("MM/dd/yyyy");

               if (tbl.IS_SAMPLE == 1) chkIS_SAMPLE.Checked = true;
               if (tbl.PARTIAL_PAYMENT == 1) chkPARTIAL_PAYMENT.Checked = true;
            }
        }
            
    
    }
}